package com.jason.concurrent.CountDownLatch;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author  yslai
 * @Date  2022/9/19 17:01
 * @Description 循环栅栏
 */
public class CycliBairrierDemo {

    private static int number = 7;
//    private int number = 7;
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(number,()->{

            System.out.println("已经集齐七个龙珠");
        });

        for (int i = 0; i < 7; i++) {
            new Thread(()->{
                System.out.println("已经集齐:"+Thread.currentThread().getName());
                try {
                    cyclicBarrier.await(); //这里做判断如果没有集齐多少个线程则不会开启这个上面的方法
                } catch (InterruptedException e) {


                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
